<template>
  <div>

<!-- 查询-->
    <el-form  v-show="isShow" :inline="true" :model="dept" class="demo-form-inline" style="text-align: left">
      <el-form-item label="部门名称" style="font-weight :1000;"  >
        <el-input v-model="dept.deptName" placeholder="请输入部门名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" style="font-weight :1000;">
        <el-select v-model="dept.status" placeholder="部门状态" clearable>
          <el-option label="正常" value="0">正常</el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="list">搜索</el-button>
        <el-button type="primary"   plain icon="el-icon-search" @click="replace">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="button-container">
    <el-button    @click="openAdd()" class="inline-button"  type="primary" plain size="small" icon="el-icon-plus">新增</el-button>

    <el-button   type="info" plain class="custom-gray-button" size="small" icon="el-icon-sort"   @click="toggleExpand">
      {{ allTable ? '全部收起' : '全部展开' }}
    </el-button>

    <el-tooltip class="item" effect="dark" content="刷新" placement="top">
      <el-button  icon="el-icon-refresh" @click="shuaxin()"></el-button>
    </el-tooltip>
    <el-tooltip class="item" effect="dark" content="隐藏搜索" placement="top" >
      <el-button icon="el-icon-search" @click="search_con"></el-button>
    </el-tooltip>
    </div>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="deptId"
        border
        v-if="refTable"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        :default-expand-all="allTable"
    >
      <el-table-column
          prop="deptName"
          label="部门名称"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="orderNum"
          label="排序"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          sortable
          width="180">
        <template v-slot="scope">
          <el-tag size="medium" v-if="scope.row.status==0">正常</el-tag>
          <el-tag size="medium" v-if="scope.row.status==1">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="180">
        <template slot-scope="scope" >
          <el-button @click="openAdd()" class="inline-button" type="text" size="small" icon="el-icon-plus">新增</el-button>
          <el-button class="inline-button" type="text" size="small" @click="openEdit(scope.row)" icon="el-icon-edit">修改</el-button>
          <el-button class="inline-button" type="text" size="small" @click="deleted(scope.row.deptId)" icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


<!--    添加-->

    <el-dialog title="添加部门" style="text-align: left" :visible.sync="dialogFormVisible" :rules="rules" ref="ruleForm" >
      <el-form :model="form">
        <el-form-item label="上级部门" style="font-weight :1000;" prop="ancestors" :label-width="formLabelWidth">
         <div class="block">
          <el-cascader
              v-model="form.parentId"
              :options="options"
              :props="{ checkStrictly: true,expandTrigger:'hover',value: 'deptId',label:'deptName'}"
              clearable
            @change="handleChangesss">
          </el-cascader>
        </div>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName" style="font-weight :1000;" :label-width="formLabelWidth" >
          <el-input v-model="form.deptName" autocomplete="off" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum" style="font-weight :1000;" :label-width="formLabelWidth">
          <el-input-number v-model="form.orderNum"  :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="负责人" prop="leader" style="font-weight :1000;" :label-width="formLabelWidth">
          <el-input v-model="form.leader" autocomplete="off" placeholder="请输入负责人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话"  prop="phone" style="font-weight :1000;" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" style="font-weight :1000;" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="部门状态" style="font-weight :1000;" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "DeptView",
  data() {
    return {
      tableData:[],
      dept:{},
      form:{},
      formLabelWidth:"120px",
      dialogFormVisible:false,
      options:[],
      ancestors:'',
      pid:'',
      refTable: true,
      allTable: false,
      isShow:true,
      deptId:this.deptId,
      rules: {
        deptName: [
          {required: true, message: '部门名称不能为空', trigger: 'blur'},
        ],
        orderNum: [
          {required: true, message: '显示排序不能为空', trigger: 'blur'},
        ],
      }
      }
  },methods: {
    replace(){
       this.dept={};
    },
    search_con(){
       this.isShow = !this.isShow
    },
    shuaxin(){
       this.list();
    },
    deleted(deptId){
        this.axios.post("http://localhost:8000/carsystem/sysDept/delete?id="+deptId).then(res=>{
             if(res.data.code==200){
                 this.$message.success(res.data.msg);
                 this.list();
             }else {
                 this.$message.error(res.data.msg);
             }
        })
    },
    openEdit(row){
       this.parentId=row.deptId;
       this.ancestors=row.ancestors;
       this.form=row;
       this.dialogFormVisible=true;
    },
    // 切换展开/收起状态
    toggleExpand() {
      this.allTable = !this.allTable;
      this.refTable = false; // 先隐藏表格
      this.$nextTick(() => {
        this.refTable = true; // 等待下一次 DOM 更新后显示表格，触发重新渲染
      });
    },

     list(){
         this.axios.post("http://localhost:8000/carsystem/sysDept/treeList",this.dept).then(res=>{
           console.log(res);
             this.tableData=res.data;
             this.options=res.data;
           console.log(res.data);
         })
     },

    handleChangesss(value){
      this.ancestors=value.join(",");
      this.pid=value[value.length-1]
      console.log(this.ancestors);
      console.log(this.pid)
    },
    //表单提交
    addOrUpdate(){
      let id = this.form.deptId;
      if(id){//前端判断  如果有值 就是true   没有值(null '' undefind)即使false
        //做修改
        this.update();
      }else {
        //做添加
        this.add();
      }
    },
    add(){
      this.form.ancestors="0,"+this.ancestors;
      this.form.parentId=this.pid;
      this.axios.post("http://localhost:8000/carsystem/sysDept/addSave",this.form).then(res=> {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.dialogFormVisible = false;
              this.list();
            } else {
              this.$message.error(res.data.msg);
            }
          }
      )

    },



    //修改
    update(){
      this.axios.post("http://localhost:8000/carsystem/sysDept/update",this.form).then(res=>{
        if (res.data.code==200){
          this.$message.success(res.data.msg);
          this.dialogFormVisible=false;
          this.list();
        }else {
          this.$message.error(res.data.msg);
        }
      })
    },


    openAdd(){

      this.dialogFormVisible=true;
      this.form={};
    },
  },created() {
    this.list();
  }


}
</script>

<style scoped>
.button-container {
  display: flex;
  justify-content: flex-start;  /* 将按钮靠左对齐 */
  gap: 10px;  /* 调整按钮之间的间距 */
}

.item {
  margin: 4px;
}
</style>